function [labels_map,seg_labels_tables]=segment_labels(seg_map, reg_labels,seg_indices,reg_indices_cell,confidence_cell)

% This function calculates the last map of labels. its getting all the region's voting, and according 
% to it the function calculate for each segment (and pixel) it's label.

% Function Inputs:
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% seg_map - map of segments. 
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% reg_labels - cell of region' labels , in number of all region' hypothesis.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% seg_indices - cell of all indices per segment.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% reg_indices_cell - cell of all indices of regions per hypothesis.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%  confidence_cell - cell array with confidence number for each region label' decision. 
% if the classifier won't give confidences, then confidence=0.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

% Function Outputs:
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% labels_map - map of labels.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% seg_labels_tables - 3 tables of voting, each table is labelsXseg_count. there is a table for polling 
% (max votes count), max_value (maximum confidence value) and sum_value (sum of confidence values).
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% Global Inputs:
% label_p.segment_voting_style

global label_p 
voting_style=label_p.segment_voting_style;

%reshaping the indices and labels
new_reg_labels=reg_labels{1};
for i=2:size(reg_labels,1)
    new_reg_labels=[new_reg_labels; reg_labels{i}];
end
reg_labels=new_reg_labels;
clear new_reg_labels;

new_reg_indices=reg_indices_cell{1};
for i=2:size(reg_indices_cell,1)
    new_reg_indices=[new_reg_indices; reg_indices_cell{i}];
end
reg_indices_cell=new_reg_indices;
clear new_reg_indices;

new_reg_conf=confidence_cell{1};
for i=2:size(confidence_cell,1)
    new_reg_conf=[new_reg_conf; confidence_cell{i}];
end
reg_conf=new_reg_conf;
clear new_reg_conf;

%setting some variables
num_maps=size(reg_labels,1); % num of hypothesises
num_seg=size(unique(seg_map),1); % num of segments
[seg_table_polling,seg_table_max_value,seg_table_sum_value]=deal(zeros(5,num_seg)); % table of segment and their votes from each hypothesis by label

% creating 3 tables of voting, at the end we will choose only one to use
for i=1:num_maps
    reg_label=reg_labels{i};
    reg_label(reg_label==0)=5;
    reg_count=length(reg_label);
    for j=1:reg_count
        reg_indices=reg_indices_cell{i}{j};
        seg_list=unique(seg_map(reg_indices));
        label=reg_label(j);
        seg_table_polling(label,seg_list)=seg_table_polling(label,seg_list)+1;
        old_conf=seg_table_max_value(label,seg_list);
        new_conf=reg_conf{i}(j).*ones(size(seg_list))';
        seg_table_max_value(label,seg_list)=max(old_conf,new_conf);
        seg_table_sum_value(label,seg_list)=seg_table_sum_value(label,seg_list)+new_conf;;
    end
end

% choosing the right table to vote the segements from it 
switch voting_style
    case 'polling'
        seg_labels_table=seg_table_polling;
    case 'max_value' 
        seg_labels_table=seg_table_max_value;
    case 'sum_value' 
        seg_labels_table=seg_table_sum_value;
end
seg_labels_tables.polling=seg_table_polling;
seg_labels_tables.max_value=seg_table_max_value;
seg_labels_tables.sum_value=seg_table_sum_value;
%now we got labelsXseg_count mat of labels, need to poll now the right
%label for each segment
seg_labels=zeros(1,num_seg);
labels_map=seg_map;
for i=1:num_seg
    labels=seg_labels_table(:,i); % all votes for segment
    candidate=max(labels);
    label_indices=find(labels==candidate);
    if (length(label_indices)==1)                          % meaning we have one winner
        seg_labels(i)=label_indices(1);         % the right label
    else 
        seg_labels(i)=0;
    end
    if  (seg_labels(i)==5)          % switching back to 0=undefined
        seg_labels(i)=0;
    end
    labels_map(seg_indices{i})=seg_labels(i);
end

